<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/13
 * Time: 16:12
 */

namespace App\CronTask;


use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\HttpClient\HttpClient;
//更新游戏状态
class CheckMatchStatusCron extends AbstractCronTask
{
    public static function getRule(): string
    {
        // TODO: Implement getRule() method.
        //return '@daily';
        return '*/1 * * * *';
    }
    public static function getTaskName(): string
    {
        // TODO: Implement getTaskName() method.
        return 'CheckMatchStatus';
    }
    static function run(\swoole_server $server, int $taskId, int $fromWorkerId, $flags = null)
    {
        $tables = ['siam_csgomatchs'];
        $path = [UrlEnums::CSGO_BASICINOF_MATCH_PATH];
        for ( $i=0;$i<count($path);$i++ ){
            MysqlPool::invoke(function (MysqlObject $db)use($tables,$path,$i){
                $matchs = $db->whereIn('status',[2])->get($tables[$i]);
                list($s1, $s2) = explode(' ', microtime());
                $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
                foreach ( $matchs as $match ){
                    if( $microtime >= $match['start_time'] ){ //更新游戏状态
                        $url = UrlEnums::FJURL.$path[$i]."?match_id={$match['match_id']}&version=2";
                        $client = new HttpClient($url);
                        $option = [
                            'path'      => $path[$i],
                            'api_time'  => $microtime
                        ];
                        $header = Auth::getInstance()->header($option);
                        $client->setHeaders($header,true,false);
                        $response = $client->get();
                        $content = $response->getBody();
                        $data = json_decode($content,true);
                        if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
                            $status = $data['data']['status'];
                            $battle_ids = json_encode($data['data']['battle_ids']);
                            $team_a_score = $data['data']['team_a_score'];
                            $team_b_score = $data['data']['team_b_score'];
                            $udata = [
                                'status'     => $status,
                                'battle_ids' => $battle_ids,
                                'team_a_score'=> $team_a_score,
                                'team_b_score'=> $team_b_score
                            ];
                            if( isset($data['data']['battle_list']) && $data['data']['battle_list'] ){
                                $map_names = [];
                                foreach ( $data['data']['battle_list'] as $k=>$v ){
                                    $map_names[] = strtolower('de_'.$v['map_name']);
                                }
                                $udata['map_names'] = json_encode($map_names);
                                $udata['battle_list'] = json_encode($data['data']['battle_list']);
                            }
                            $res = $db->where('match_id',$match['match_id'])->update($tables[$i],$udata);
                        }

                    }
                }
            });
        }

    }
}